package com.thwcompany.controller;
import com.thwcompany.pojo.Mail;
import com.thwcompany.pojo.TopicMail;
import com.thwcompany.service.ProducerModeService;
import com.thwcompany.service.PublisherModeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
 * Created by IntelliJ IDEA2021.3
 * @Author: Tenghw
 * @Date: 2022/03/26  16:45
 * @Description:
 */
@Controller
@Slf4j
@SuppressWarnings("all")
public class RabbitMQController {

    @Autowired
    ProducerModeService producerModeService;
    @Autowired
    PublisherModeService publisherModeService;

    //===================================================================================
    //生产者直接向消息队列myqueue发送消息,
    // 生产者——消费者模型：添加了一个队列，并创建了两个消费者用于监听队列消息，
    // 当有消息到达时，两个消费者会交替收到消息。这一过程没有创建交换机，
    // 但会使用默认的direct直连交换机
    @RequestMapping(value="/produce",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public void produce(@ModelAttribute("mail") Mail mail) throws Exception{
        producerModeService.sendMail("myqueue",mail);
    }
    //===================================================================================
    //发布-订阅模式，用的是fanout交换机
    @RequestMapping(value="/topic",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public void topic(@ModelAttribute("mail")Mail mail) throws Exception{
        publisherModeService.publishMail(mail);
    }
    //===================================================================================
    //发布-订阅模式，用的是directt交换机
    @RequestMapping(value="/direct",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public void direct(@ModelAttribute("mail") TopicMail topicMail){
        Mail m=new Mail(topicMail.getMailId(),topicMail.getCountry(),topicMail.getWeight());
        publisherModeService.senddirectMail(m, topicMail.getRoutingkey());
    }
    //===================================================================================
    //发布-订阅模式，用的是topic交换机
    @RequestMapping(value="/mytopic",produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public void topic(@ModelAttribute("mail")TopicMail topicMail){
        Mail m=new Mail(topicMail.getMailId(),topicMail.getCountry(),topicMail.getWeight());
        publisherModeService.sendtopicMail(m, topicMail.getRoutingkey());
    }
    //===================================================================================


}
